/* 
    type  Point,LineString,Circle,Polygon
    Square Circle
    Rectangle LineString
     */
/**
 *
 * @param {String} type Point,LineString,Circle,Polygon,Square,Rectangle
 * @param {Object} source
 * @param {function} callback  处理绘制成功之后的回调
 */

//封装函数
function createDraw({
  // 参数
  type = 'Point',
  source,
  callback,
}) {
  // 初始值
  let maxPoints = null
  let geometryFunction = null
  // 矩形
  if (type == 'Rectangle') {
    type = 'LineString'
    maxPoints = 2
    geometryFunction = function (coordinates, geometry) {
      if (!geometry) {
        //多边形
        geometry = new ol.geom.Polygon(null)
      }
      var start = coordinates[0]
      var end = coordinates[1]
      geometry.setCoordinates([
        [start, [start[0], end[1]], end, [end[0], start[1]], start],
      ])
      return geometry
    }
    // 正方形
  } else if (type == 'Square') {
    type = 'Circle'
    geometryFunction = ol.interaction.Draw.createRegularPolygon(4)
  }
  // 返回值
  let draw = new ol.interaction.Draw({
    type,
    source,
    geometryFunction,
    maxPoints,
  })
  // 如果有回调函数，绑定地图事件
  if (callback) {
    draw.on('drawend', callback)
  }
  // 调用时直接激活就好
  return draw
}
